<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>serial: examples/serial_example.cc File Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">serial
   &#160;<span id="projectnumber">1.1.0</span>
   </div>
   <div id="projectbrief">Cross-platform, serial port library written in C++</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_d28a4824dc47e487b107a5db32ef43c4.html">examples</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="summary">
<a href="#func-members">Functions</a>  </div>
  <div class="headertitle">
<div class="title">serial_example.cc File Reference</div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><code>#include &lt;string&gt;</code><br />
<code>#include &lt;iostream&gt;</code><br />
<code>#include &lt;cstdio&gt;</code><br />
<code>#include &lt;unistd.h&gt;</code><br />
<code>#include &quot;<a class="el" href="serial_8h_source.html">serial/serial.h</a>&quot;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for serial_example.cc:</div>
<div class="dyncontent">
<div class="center"><img src="serial__example_8cc__incl.png" border="0" usemap="#examples_2serial__example_8cc" alt=""/></div>
<map name="examples_2serial__example_8cc" id="examples_2serial__example_8cc">
<area shape="rect" id="node6" href="serial_8h.html" title="serial/serial.h" alt="" coords="329,80,427,107"/>
<area shape="rect" id="node13" href="v8stdint_8h.html" title="serial/v8stdint.h" alt="" coords="643,155,758,181"/>
</map>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a89b7c9d8c710b057346cc9ac52ae3734"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="serial__example_8cc.html#a89b7c9d8c710b057346cc9ac52ae3734">my_sleep</a> (unsigned long milliseconds)</td></tr>
<tr class="separator:a89b7c9d8c710b057346cc9ac52ae3734"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a996e0d351ea6c804947e9533581765ea"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="serial__example_8cc.html#a996e0d351ea6c804947e9533581765ea">enumerate_ports</a> ()</td></tr>
<tr class="separator:a996e0d351ea6c804947e9533581765ea"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae5ad5cbeccaedc03a48d3c7eaa803e79"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="serial__example_8cc.html#ae5ad5cbeccaedc03a48d3c7eaa803e79">print_usage</a> ()</td></tr>
<tr class="separator:ae5ad5cbeccaedc03a48d3c7eaa803e79"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ac1f545534cdaab9094198a5dc2c2a79f"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="serial__example_8cc.html#ac1f545534cdaab9094198a5dc2c2a79f">run</a> (int argc, char **argv)</td></tr>
<tr class="separator:ac1f545534cdaab9094198a5dc2c2a79f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="serial__example_8cc.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a> (int argc, char **argv)</td></tr>
<tr class="separator:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<h2 class="groupheader">Function Documentation</h2>
<a class="anchor" id="a996e0d351ea6c804947e9533581765ea"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void enumerate_ports </td>
          <td>(</td>
          <td class="paramname"></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<div class="fragment"><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;{</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;    vector&lt;serial::PortInfo&gt; devices_found = <a class="code" href="namespaceserial.html#a8fa048a9e4663d6d6b474c1830491a57">serial::list_ports</a>();</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;    vector&lt;serial::PortInfo&gt;::iterator iter = devices_found.begin();</div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;    <span class="keywordflow">while</span>( iter != devices_found.end() )</div><div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;    {</div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;        <a class="code" href="structserial_1_1_port_info.html">serial::PortInfo</a> device = *iter++;</div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;</div><div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;        printf( <span class="stringliteral">&quot;(%s, %s, %s)\n&quot;</span>, device.<a class="code" href="structserial_1_1_port_info.html#a5d4242cdd6c0d01260e24964af4c23d2">port</a>.c_str(), device.<a class="code" href="structserial_1_1_port_info.html#a2ba37dd33d47b554aef5c15c1fe8b872">description</a>.c_str(),</div><div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;     device.<a class="code" href="structserial_1_1_port_info.html#a7d55368e1a4e6ccc9da6f4d339524837">hardware_id</a>.c_str() );</div><div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;    }</div><div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;}</div><div class="ttc" id="namespaceserial_html_a8fa048a9e4663d6d6b474c1830491a57"><div class="ttname"><a href="namespaceserial.html#a8fa048a9e4663d6d6b474c1830491a57">serial::list_ports</a></div><div class="ttdeci">std::vector&lt; PortInfo &gt; list_ports()</div></div>
<div class="ttc" id="structserial_1_1_port_info_html_a7d55368e1a4e6ccc9da6f4d339524837"><div class="ttname"><a href="structserial_1_1_port_info.html#a7d55368e1a4e6ccc9da6f4d339524837">serial::PortInfo::hardware_id</a></div><div class="ttdeci">std::string hardware_id</div><div class="ttdef"><b>Definition:</b> serial.h:759</div></div>
<div class="ttc" id="structserial_1_1_port_info_html_a2ba37dd33d47b554aef5c15c1fe8b872"><div class="ttname"><a href="structserial_1_1_port_info.html#a2ba37dd33d47b554aef5c15c1fe8b872">serial::PortInfo::description</a></div><div class="ttdeci">std::string description</div><div class="ttdef"><b>Definition:</b> serial.h:756</div></div>
<div class="ttc" id="structserial_1_1_port_info_html"><div class="ttname"><a href="structserial_1_1_port_info.html">serial::PortInfo</a></div><div class="ttdef"><b>Definition:</b> serial.h:750</div></div>
<div class="ttc" id="structserial_1_1_port_info_html_a5d4242cdd6c0d01260e24964af4c23d2"><div class="ttname"><a href="structserial_1_1_port_info.html#a5d4242cdd6c0d01260e24964af4c23d2">serial::PortInfo::port</a></div><div class="ttdeci">std::string port</div><div class="ttdef"><b>Definition:</b> serial.h:753</div></div>
</div><!-- fragment -->
</div>
</div>
<a class="anchor" id="a3c04138a5bfe5d72780bb7e82a18e627"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int main </td>
          <td>(</td>
          <td class="paramtype">int&#160;</td>
          <td class="paramname"><em>argc</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">char **&#160;</td>
          <td class="paramname"><em>argv</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">
<div class="fragment"><div class="line"><a name="l00176"></a><span class="lineno">  176</span>&#160;                                {</div><div class="line"><a name="l00177"></a><span class="lineno">  177</span>&#160;  <span class="keywordflow">try</span> {</div><div class="line"><a name="l00178"></a><span class="lineno">  178</span>&#160;    <span class="keywordflow">return</span> <a class="code" href="serial__example_8cc.html#ac1f545534cdaab9094198a5dc2c2a79f">run</a>(argc, argv);</div><div class="line"><a name="l00179"></a><span class="lineno">  179</span>&#160;  } <span class="keywordflow">catch</span> (exception &amp;e) {</div><div class="line"><a name="l00180"></a><span class="lineno">  180</span>&#160;    cerr &lt;&lt; <span class="stringliteral">&quot;Unhandled Exception: &quot;</span> &lt;&lt; e.what() &lt;&lt; endl;</div><div class="line"><a name="l00181"></a><span class="lineno">  181</span>&#160;  }</div><div class="line"><a name="l00182"></a><span class="lineno">  182</span>&#160;}</div><div class="ttc" id="serial__example_8cc_html_ac1f545534cdaab9094198a5dc2c2a79f"><div class="ttname"><a href="serial__example_8cc.html#ac1f545534cdaab9094198a5dc2c2a79f">run</a></div><div class="ttdeci">int run(int argc, char **argv)</div><div class="ttdef"><b>Definition:</b> serial_example.cc:68</div></div>
</div><!-- fragment -->
</div>
</div>
<a class="anchor" id="a89b7c9d8c710b057346cc9ac52ae3734"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void my_sleep </td>
          <td>(</td>
          <td class="paramtype">unsigned long&#160;</td>
          <td class="paramname"><em>milliseconds</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<div class="fragment"><div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;                                          {</div><div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;<span class="preprocessor">#ifdef _WIN32</span></div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;      Sleep(milliseconds); <span class="comment">// 100 ms</span></div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;<span class="preprocessor">#else</span></div><div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;      usleep(milliseconds*1000); <span class="comment">// 100 ms</span></div><div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;<span class="preprocessor">#endif</span></div><div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;}</div></div><!-- fragment -->
</div>
</div>
<a class="anchor" id="ae5ad5cbeccaedc03a48d3c7eaa803e79"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void print_usage </td>
          <td>(</td>
          <td class="paramname"></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<div class="fragment"><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;{</div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;    cerr &lt;&lt; <span class="stringliteral">&quot;Usage: test_serial {-e|&lt;serial port address&gt;} &quot;</span>;</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;    cerr &lt;&lt; <span class="stringliteral">&quot;&lt;baudrate&gt; [test string]&quot;</span> &lt;&lt; endl;</div><div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;}</div></div><!-- fragment -->
</div>
</div>
<a class="anchor" id="ac1f545534cdaab9094198a5dc2c2a79f"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int run </td>
          <td>(</td>
          <td class="paramtype">int&#160;</td>
          <td class="paramname"><em>argc</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">char **&#160;</td>
          <td class="paramname"><em>argv</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">
<div class="fragment"><div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;{</div><div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;  <span class="keywordflow">if</span>(argc &lt; 2) {</div><div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;      <a class="code" href="serial__example_8cc.html#ae5ad5cbeccaedc03a48d3c7eaa803e79">print_usage</a>();</div><div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;    <span class="keywordflow">return</span> 0;</div><div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;  }</div><div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;</div><div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;  <span class="comment">// Argument 1 is the serial port or enumerate flag</span></div><div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;  <span class="keywordtype">string</span> port(argv[1]);</div><div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;</div><div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;  <span class="keywordflow">if</span>( port == <span class="stringliteral">&quot;-e&quot;</span> ) {</div><div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;      <a class="code" href="serial__example_8cc.html#a996e0d351ea6c804947e9533581765ea">enumerate_ports</a>();</div><div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;      <span class="keywordflow">return</span> 0;</div><div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;  }</div><div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;  <span class="keywordflow">else</span> <span class="keywordflow">if</span>( argc &lt; 3 ) {</div><div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;      <a class="code" href="serial__example_8cc.html#ae5ad5cbeccaedc03a48d3c7eaa803e79">print_usage</a>();</div><div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;      <span class="keywordflow">return</span> 1;</div><div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;  }</div><div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;</div><div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;  <span class="comment">// Argument 2 is the baudrate</span></div><div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;  <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> baud = 0;</div><div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;<span class="preprocessor">#if defined(WIN32) &amp;&amp; !defined(__MINGW32__)</span></div><div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;  sscanf_s(argv[2], <span class="stringliteral">&quot;%lu&quot;</span>, &amp;baud);</div><div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;<span class="preprocessor">#else</span></div><div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;  sscanf(argv[2], <span class="stringliteral">&quot;%lu&quot;</span>, &amp;baud);</div><div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;<span class="preprocessor">#endif</span></div><div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;</div><div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;  <span class="comment">// port, baudrate, timeout in milliseconds</span></div><div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;  <a class="code" href="classserial_1_1_serial.html">serial::Serial</a> my_serial(port, baud, <a class="code" href="structserial_1_1_timeout.html#aa4fbd72e16f47c9aea9fb3c32ca17828">serial::Timeout::simpleTimeout</a>(1000));</div><div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;</div><div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160;  cout &lt;&lt; <span class="stringliteral">&quot;Is the serial port open?&quot;</span>;</div><div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;  <span class="keywordflow">if</span>(my_serial.isOpen())</div><div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;    cout &lt;&lt; <span class="stringliteral">&quot; Yes.&quot;</span> &lt;&lt; endl;</div><div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;  <span class="keywordflow">else</span></div><div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160;    cout &lt;&lt; <span class="stringliteral">&quot; No.&quot;</span> &lt;&lt; endl;</div><div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;</div><div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;  <span class="comment">// Get the Test string</span></div><div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;  <span class="keywordtype">int</span> count = 0;</div><div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;  <span class="keywordtype">string</span> test_string;</div><div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;  <span class="keywordflow">if</span> (argc == 4) {</div><div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;    test_string = argv[3];</div><div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;  } <span class="keywordflow">else</span> {</div><div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;    test_string = <span class="stringliteral">&quot;Testing.&quot;</span>;</div><div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;  }</div><div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;</div><div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;  <span class="comment">// Test the timeout, there should be 1 second between prints</span></div><div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;  cout &lt;&lt; <span class="stringliteral">&quot;Timeout == 1000ms, asking for 1 more byte than written.&quot;</span> &lt;&lt; endl;</div><div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;  <span class="keywordflow">while</span> (count &lt; 10) {</div><div class="line"><a name="l00116"></a><span class="lineno">  116</span>&#160;    <span class="keywordtype">size_t</span> bytes_wrote = my_serial.write(test_string);</div><div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;</div><div class="line"><a name="l00118"></a><span class="lineno">  118</span>&#160;    <span class="keywordtype">string</span> result = my_serial.read(test_string.length()+1);</div><div class="line"><a name="l00119"></a><span class="lineno">  119</span>&#160;</div><div class="line"><a name="l00120"></a><span class="lineno">  120</span>&#160;    cout &lt;&lt; <span class="stringliteral">&quot;Iteration: &quot;</span> &lt;&lt; count &lt;&lt; <span class="stringliteral">&quot;, Bytes written: &quot;</span>;</div><div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;    cout &lt;&lt; bytes_wrote &lt;&lt; <span class="stringliteral">&quot;, Bytes read: &quot;</span>;</div><div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;    cout &lt;&lt; result.length() &lt;&lt; <span class="stringliteral">&quot;, String read: &quot;</span> &lt;&lt; result &lt;&lt; endl;</div><div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;</div><div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;    count += 1;</div><div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;  }</div><div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;</div><div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;  <span class="comment">// Test the timeout at 250ms</span></div><div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;  my_serial.setTimeout(<a class="code" href="structserial_1_1_timeout.html#adc68e33d2f94bfa33ba1062c363b9151">serial::Timeout::max</a>(), 250, 0, 250, 0);</div><div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;  count = 0;</div><div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;  cout &lt;&lt; <span class="stringliteral">&quot;Timeout == 250ms, asking for 1 more byte than written.&quot;</span> &lt;&lt; endl;</div><div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;  <span class="keywordflow">while</span> (count &lt; 10) {</div><div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;    <span class="keywordtype">size_t</span> bytes_wrote = my_serial.write(test_string);</div><div class="line"><a name="l00133"></a><span class="lineno">  133</span>&#160;</div><div class="line"><a name="l00134"></a><span class="lineno">  134</span>&#160;    <span class="keywordtype">string</span> result = my_serial.read(test_string.length()+1);</div><div class="line"><a name="l00135"></a><span class="lineno">  135</span>&#160;</div><div class="line"><a name="l00136"></a><span class="lineno">  136</span>&#160;    cout &lt;&lt; <span class="stringliteral">&quot;Iteration: &quot;</span> &lt;&lt; count &lt;&lt; <span class="stringliteral">&quot;, Bytes written: &quot;</span>;</div><div class="line"><a name="l00137"></a><span class="lineno">  137</span>&#160;    cout &lt;&lt; bytes_wrote &lt;&lt; <span class="stringliteral">&quot;, Bytes read: &quot;</span>;</div><div class="line"><a name="l00138"></a><span class="lineno">  138</span>&#160;    cout &lt;&lt; result.length() &lt;&lt; <span class="stringliteral">&quot;, String read: &quot;</span> &lt;&lt; result &lt;&lt; endl;</div><div class="line"><a name="l00139"></a><span class="lineno">  139</span>&#160;</div><div class="line"><a name="l00140"></a><span class="lineno">  140</span>&#160;    count += 1;</div><div class="line"><a name="l00141"></a><span class="lineno">  141</span>&#160;  }</div><div class="line"><a name="l00142"></a><span class="lineno">  142</span>&#160;</div><div class="line"><a name="l00143"></a><span class="lineno">  143</span>&#160;  <span class="comment">// Test the timeout at 250ms, but asking exactly for what was written</span></div><div class="line"><a name="l00144"></a><span class="lineno">  144</span>&#160;  count = 0;</div><div class="line"><a name="l00145"></a><span class="lineno">  145</span>&#160;  cout &lt;&lt; <span class="stringliteral">&quot;Timeout == 250ms, asking for exactly what was written.&quot;</span> &lt;&lt; endl;</div><div class="line"><a name="l00146"></a><span class="lineno">  146</span>&#160;  <span class="keywordflow">while</span> (count &lt; 10) {</div><div class="line"><a name="l00147"></a><span class="lineno">  147</span>&#160;    <span class="keywordtype">size_t</span> bytes_wrote = my_serial.write(test_string);</div><div class="line"><a name="l00148"></a><span class="lineno">  148</span>&#160;</div><div class="line"><a name="l00149"></a><span class="lineno">  149</span>&#160;    <span class="keywordtype">string</span> result = my_serial.read(test_string.length());</div><div class="line"><a name="l00150"></a><span class="lineno">  150</span>&#160;</div><div class="line"><a name="l00151"></a><span class="lineno">  151</span>&#160;    cout &lt;&lt; <span class="stringliteral">&quot;Iteration: &quot;</span> &lt;&lt; count &lt;&lt; <span class="stringliteral">&quot;, Bytes written: &quot;</span>;</div><div class="line"><a name="l00152"></a><span class="lineno">  152</span>&#160;    cout &lt;&lt; bytes_wrote &lt;&lt; <span class="stringliteral">&quot;, Bytes read: &quot;</span>;</div><div class="line"><a name="l00153"></a><span class="lineno">  153</span>&#160;    cout &lt;&lt; result.length() &lt;&lt; <span class="stringliteral">&quot;, String read: &quot;</span> &lt;&lt; result &lt;&lt; endl;</div><div class="line"><a name="l00154"></a><span class="lineno">  154</span>&#160;</div><div class="line"><a name="l00155"></a><span class="lineno">  155</span>&#160;    count += 1;</div><div class="line"><a name="l00156"></a><span class="lineno">  156</span>&#160;  }</div><div class="line"><a name="l00157"></a><span class="lineno">  157</span>&#160;</div><div class="line"><a name="l00158"></a><span class="lineno">  158</span>&#160;  <span class="comment">// Test the timeout at 250ms, but asking for 1 less than what was written</span></div><div class="line"><a name="l00159"></a><span class="lineno">  159</span>&#160;  count = 0;</div><div class="line"><a name="l00160"></a><span class="lineno">  160</span>&#160;  cout &lt;&lt; <span class="stringliteral">&quot;Timeout == 250ms, asking for 1 less than was written.&quot;</span> &lt;&lt; endl;</div><div class="line"><a name="l00161"></a><span class="lineno">  161</span>&#160;  <span class="keywordflow">while</span> (count &lt; 10) {</div><div class="line"><a name="l00162"></a><span class="lineno">  162</span>&#160;    <span class="keywordtype">size_t</span> bytes_wrote = my_serial.write(test_string);</div><div class="line"><a name="l00163"></a><span class="lineno">  163</span>&#160;</div><div class="line"><a name="l00164"></a><span class="lineno">  164</span>&#160;    <span class="keywordtype">string</span> result = my_serial.read(test_string.length()-1);</div><div class="line"><a name="l00165"></a><span class="lineno">  165</span>&#160;</div><div class="line"><a name="l00166"></a><span class="lineno">  166</span>&#160;    cout &lt;&lt; <span class="stringliteral">&quot;Iteration: &quot;</span> &lt;&lt; count &lt;&lt; <span class="stringliteral">&quot;, Bytes written: &quot;</span>;</div><div class="line"><a name="l00167"></a><span class="lineno">  167</span>&#160;    cout &lt;&lt; bytes_wrote &lt;&lt; <span class="stringliteral">&quot;, Bytes read: &quot;</span>;</div><div class="line"><a name="l00168"></a><span class="lineno">  168</span>&#160;    cout &lt;&lt; result.length() &lt;&lt; <span class="stringliteral">&quot;, String read: &quot;</span> &lt;&lt; result &lt;&lt; endl;</div><div class="line"><a name="l00169"></a><span class="lineno">  169</span>&#160;</div><div class="line"><a name="l00170"></a><span class="lineno">  170</span>&#160;    count += 1;</div><div class="line"><a name="l00171"></a><span class="lineno">  171</span>&#160;  }</div><div class="line"><a name="l00172"></a><span class="lineno">  172</span>&#160;</div><div class="line"><a name="l00173"></a><span class="lineno">  173</span>&#160;  <span class="keywordflow">return</span> 0;</div><div class="line"><a name="l00174"></a><span class="lineno">  174</span>&#160;}</div><div class="ttc" id="serial__example_8cc_html_ae5ad5cbeccaedc03a48d3c7eaa803e79"><div class="ttname"><a href="serial__example_8cc.html#ae5ad5cbeccaedc03a48d3c7eaa803e79">print_usage</a></div><div class="ttdeci">void print_usage()</div><div class="ttdef"><b>Definition:</b> serial_example.cc:62</div></div>
<div class="ttc" id="serial__example_8cc_html_a996e0d351ea6c804947e9533581765ea"><div class="ttname"><a href="serial__example_8cc.html#a996e0d351ea6c804947e9533581765ea">enumerate_ports</a></div><div class="ttdeci">void enumerate_ports()</div><div class="ttdef"><b>Definition:</b> serial_example.cc:47</div></div>
<div class="ttc" id="structserial_1_1_timeout_html_adc68e33d2f94bfa33ba1062c363b9151"><div class="ttname"><a href="structserial_1_1_timeout.html#adc68e33d2f94bfa33ba1062c363b9151">serial::Timeout::max</a></div><div class="ttdeci">static uint32_t max()</div><div class="ttdef"><b>Definition:</b> serial.h:102</div></div>
<div class="ttc" id="classserial_1_1_serial_html"><div class="ttname"><a href="classserial_1_1_serial.html">serial::Serial</a></div><div class="ttdef"><b>Definition:</b> serial.h:147</div></div>
<div class="ttc" id="structserial_1_1_timeout_html_aa4fbd72e16f47c9aea9fb3c32ca17828"><div class="ttname"><a href="structserial_1_1_timeout.html#aa4fbd72e16f47c9aea9fb3c32ca17828">serial::Timeout::simpleTimeout</a></div><div class="ttdeci">static Timeout simpleTimeout(uint32_t timeout)</div><div class="ttdef"><b>Definition:</b> serial.h:112</div></div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Mon Aug 20 2018 10:52:41 for serial by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.11
</small></address>
</body>
</html>
